package com.redteam.xomtro.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.redteam.xomtro.MasterActivity;
import com.redteam.xomtro.R;
import com.redteam.xomtro.RoomDetailActivity;
import com.redteam.xomtro.classes.Support;
import com.redteam.xomtro.entities.Room;

import java.util.ArrayList;
import java.util.List;

public class ProfileTab_Room extends PlaceTabFragment{
    private float numOfColumns = 2;
    private ArrayList<Room> roomList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View r = inflater.inflate(R.layout.fragment_profile_room, container, false);
        assert r != null;
        if (Support.isTablet(activity)) {
            numOfColumns = 3;
        }

        roomList = Room.getListRoomExample();
        GridView grid_view = (GridView) r.findViewById(R.id.grid_view);
        float rowCount = (float) Math.ceil(roomList.size() / numOfColumns);
        int totalHeight = (int) Support.dpToPixel((rowCount * (Support.getScaledDensity(activity) * 100f)), activity);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, totalHeight);
        grid_view.setLayoutParams(layoutParams);
        grid_view.setNumColumns((int) numOfColumns);
        RoomListSmallAdapter adapter = new RoomListSmallAdapter(roomList);
        grid_view.setAdapter(adapter);
        return r;
    }

    class RoomListSmallAdapter extends BaseAdapter {

        private final List<Room> roomList;

        public RoomListSmallAdapter(List<Room> roomList) {
            this.roomList = roomList;
        }

        @Override
        public View getView(final int position, View v, ViewGroup parent) {
            if (v == null)
                v = View.inflate(activity, R.layout.item_room_small, null);
            imageLoader.displayImage(roomList.get(position).getBannerUrl(), ((ImageView) v.findViewById(R.id.ivRoomImg)), options);
//            ((TextView) v.findViewById(R.id.tvEventTitle)).setText(roomList.get(position).getRoom_Name());
//            ((TextView) v.findViewById(R.id.tvEventDescription)).setText(roomList.get(position).getRoom_Description());
//            v.findViewById(R.id.btnEdit).setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    Intent it = new Intent(activity, RoomDetailActivity.class);
//                    it.putExtra(RoomDetailActivity.SELECTED_ROOM, roomList.get(position));
//                    startActivity(it);
//                }
//            });
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent it = new Intent(activity, RoomDetailActivity.class);
                    it.putExtra(RoomDetailActivity.SELECTED_ROOM, roomList.get(position));
                    startActivity(it);
                }
            });
            return v;
        }

        @Override
        public int getCount() {
            return roomList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }
    }

    public ProfileTab_Room newInstance(MasterActivity mainActivity) {
        super.newInstance(mainActivity);
        return this;
    }
}
